#!/bin/sh
#Auto Make install MFS Files
#Author Wugk 2013-12-04
cat <<EOF
++++++++++++++++Welcome To Use Auto Install MFS Scripts ++++++++++++++++++
+++++++++++++++++++++This MFS Install Version 1.6.27++++++++++++++++++++++
+++++++++++++++++++++++++2013-12-04 Author wugk ++++++++++++++++++++++++++
EOF

#Defined Variables
SRC_DIR=/usr/src/
MFS_COUNT=(
	http://www.moosefs.org/tl_files/mfscode
	mfs-1.6.27-1.tar.gz
	mfs-1.6.27
	'''./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var/lib --with-default-user=mfs --with-default-group=mfs --disable-mfschunkserver --disable-mfsmount'''
	'''./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var/lib --with-default-user=mfs --with-default-group=mfs --disable-mfsmaster'''
	'''./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var/lib --with-default-user=mfs --with-default-group=mfs --disable-mfsmaster --disable-mfschunkserver'''
)


##Defined Function Command Master MFS Install
master_mfs ()
{

	if [ -e /usr/sbin/mfsmaster ];then
	echo "This Mfsmaster already exists,Please exit "
	exit 0
	fi

	#Download MFS SOft Version 1.6.27
	cd $SRC_DIR ; wget -c ${MFS_COUNT[0]}/${MFS_COUNT[1]} &&echo "Download ${MFS_COUNT[1]} Successfully !"

	#Configure MFS and make install and configuration MFS 
	groupadd mfs ;useradd -g mfs mfs
	tar -xzf ${MFS_COUNT[1]} ;cd ${MFS_COUNT[2]} &&${MFS_COUNT[3]} &&make &&make install
	[ $? == 0 ]&&echo "This MFS ${MFS_COUNT[2]} Install Successfully,Please Wait Config Master_MFS ......"
	cd /etc/mfs/ &&
	cp mfsmaster.cfg.dist mfsmaster.cfg
	cp mfsmetalogger.cfg.dist mfsmetalogger.cfg
	cp mfsexports.cfg.dist mfsexports.cfg
	cd /var/lib/mfs ;mv metadata.mfs.empty metadata.mfs
}


##Defined Function Command Chunks MFS Install
chunks_mfs ()

{
	if [ -e /usr/sbin/mfschunkserver ];then
	echo "This Mfschunkserver already exists,Please exit "
	exit 0
	fi
	#Download MFS SOft Version 1.6.27
        cd $SRC_DIR ; wget -c ${MFS_COUNT[0]}/${MFS_COUNT[1]} &&echo "Download ${MFS_COUNT[1]} Successfully !"

	#Configure MFS and make install and configuration MFS 
        groupadd mfs ;useradd -g mfs mfs
        tar -xzf ${MFS_COUNT[1]} ;cd ${MFS_COUNT[2]} &&${MFS_COUNT[4]} &&make &&make install
        [ $? == 0 ]&&echo "This MFS ${MFS_COUNT[2]} Install Successfully,Please Wait Config Chunk_MFS ......"
	cd /etc/mfs/ &&
	cp mfschunkserver.cfg.dist mfschunkserver.cfg
	cp mfshdd.cfg.dist mfshdd.cfg
	mkdir -p /data/{mfs1,mfs2}
	echo -e "/data/mfs1\n/data/mfs2" >>mfshdd.cfg
	chown -R mfs:mfs /data/mfs1
	chown -R mfs:mfs /data/mfs2
}

client_mfs ()

{
	if [ -e /usr/bin/mfsmount ];then
 	echo "This Mfschunkserver already exists,Please exit "
	exit 0
	fi
	#Download MFS SOft Version 1.6.27
        cd $SRC_DIR ; wget -c ${MFS_COUNT[0]}/${MFS_COUNT[1]} &&echo "Download ${MFS_COUNT[1]} Successfully !"

	#Configure MFS and make install and configuration MFS 
        groupadd mfs ;useradd -g mfs mfs
	yum install fuse-devel -y 
        tar -xzf ${MFS_COUNT[1]} ;cd ${MFS_COUNT[2]} &&${MFS_COUNT[5]} &&make &&make install
        [ $? == 0 ]&&echo -e "This MFS ${MFS_COUNT[2]} Client Install Successfully\nYou Can Use :{/usr/bin/mfsmount /mnt/ -H mfs_master_ip} to Mount !"
}

case  $1 in 

	master_mfs )
		master_mfs
		;;
	chunks_mfs )
        chunks_mfs
        ;;
	client_mfs )
       	client_mfs 
        ;;
	* 	)
	echo
		echo "$0 {master_mfs | chunks_mfs | client_mfs}"
		;;
esac
